<?php
session_start();

require './includes/functions.php';

if (!isset($_SESSION['user']) || (!isset($_GET['id_participant']) && !isset($_POST['id_participant']))) {
    redirect_to("../401.php");
}

if (isset($_POST['id'])) {

    $id = $_POST['id'];
    $date_naissance = $_POST['date_naissance'];
    $nom_prenom = $_POST['nom_prenom'];

    $idP = $_POST['id_participant'];
    $nomP = $_POST['nomP'];
    $prenomP = $_POST['prenomP'];
    $nb_accompagnants = $_POST['nb_accompagnants'];

    $page = "./adm_accompagnants.php?id=" . $idP . "&nom=" . $nomP . "&nb_accompagnants=".$nb_accompagnants."&prenom=" . $prenomP;

    require './includes/db.php';
    echo "<link rel=\"stylesheet\" href=\"./css/screen.css\" media=\"screen\" />";

    $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
    $mysqli->set_charset("utf8");

    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL with MySQLI: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }



    if ($id == -1) {

        


        $sql = "insert into and_accompagnants (nom_prenom, date_naissance, id_participant) values ('$nom_prenom','$date_naissance', $idP)";
        $msg = "Accompagnant ajout&eacute;";
        $msg_err = "Erreur d'ajout de l'accompagnant !!";
    } else {

        $sql = "update and_accompagnants set nom_prenom='$nom_prenom', date_naissance='$date_naissance' where id =$id";
        $msg = "Accompagnant modifi&eacute;";
        $msg_err = "Erreur lors de la modification de l'accompagnant !!";
    }


    $mysqli->query($sql) or die('Problem executing update/insert query ' . $mysqli->error . '<br />'.$sql);


    if ($mysqli->affected_rows > 0) {

        if ($id == -1) {
            $nb_accompagnants++;
            $mysqli->query("update and_participant set nb_accompagnants=" . $nb_accompagnants . " where id=" . $idP) or die('Problem updating nb_accompagnts lors de l\'insertion' . $mysqli->error);
        }

        echo "<br /><br /><h1 style=\"color: font-family: 'PT Sans Narrow'\"><b style=\"color: yellowgreen;\">$msg</b></h1><h4>Redirection vers la page accompagnant dans <b><span id='compteur' class='compteur'>";
        ?>

        <script language='javascript'>

            function timestamp(t)
            {
                document.getElementById("compteur").innerHTML=(t/1000);
                t-=1000;
                if(t==0)
                    self.location="<?php echo $page; ?>";
                                                                                                                                                                                                                    		 
                setTimeout("timestamp("+t+")",1000);
            }
            window.onload = timestamp(2000);
        </script>
        <?php
        echo "</span></b> secondes</h4><img src='./adm-auth/images/superdroid.png' /></div><div class='bas' id='bas'></div>";
    } else {

        echo "<br /><br /><h1><b style=\"color: rgb(230, 0, 17);\">$msg_err</b></h1><h4>Redirection vers la page accompagnant dans <b><span id='compteur' class='compteur'>";
        ?>

        <script language='javascript'>

            function timestamp(t)
            {
                document.getElementById("compteur").innerHTML=(t/1000);
                t-=1000;
                if(t==0)
                    self.location="<?php echo $page; ?>";
                                                                                                                                                                                                                    		 
                setTimeout("timestamp("+t+")",1000);
            }
            window.onload = timestamp(2000);
        </script>
        <?php
        echo "</span></b> secondes</h4><img src='./adm-auth/images/superdroid.png' /></div><div class='bas' id='bas'></div>";
    }

    $mysqli->close();
} else {

    require './includes/db.php';


    if (isset($_GET['id']) && !isset($_GET['nom_prenom'])) {


        echo "<link rel=\"stylesheet\" href=\"./css/screen.css\" media=\"screen\" />";

        $id = $_GET['id'];
        $idP = $_GET['id_participant'];
        $nomP = $_GET['nomP'];
        $prenomP = $_GET['prenomP'];
        $nb_accompagnants = $_GET['nb_accompagnants'];

        $page = "./adm_accompagnants.php?id=" . $idP . "&nom=" . $nomP . "&nb_accompagnants=".$nb_accompagnants."&prenom=" . $prenomP;


        $mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);
        $mysqli->set_charset("utf8");

        if ($mysqli->connect_errno) {
            echo "Failed to connect to MySQL with MySQLI: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
        }


        $sql = "delete from and_accompagnants where id = " . $id;

        $mysqli->query($sql) or die('Problem deleting' . $mysqli->errno);


        if ($mysqli->affected_rows > 0) {


            $nb_accompagnants--;
            $mysqli->query("update and_participant set nb_accompagnants=" . $nb_accompagnants . " where id=" . $idP) or die('Problem deleting' . $mysqli->errno);



            echo "<br /><br /><h1 style=\"color: font-family: 'PT Sans Narrow'\"><b style=\"color: yellowgreen;\">Accompagnant supprim&eacute;e</b></h1><h4>Redirection vers la page accompagnants dans <b><span id='compteur' class='compteur'>";
            ?>

            <script language='javascript'>

                function timestamp(t)
                {
                    document.getElementById("compteur").innerHTML=(t/1000);
                    t-=1000;
                    if(t==0)
                        self.location="<?php echo $page; ?>";
                                                                                                                                                                                                                                                                                                    		 
                    setTimeout("timestamp("+t+")",1000);
                }
                window.onload = timestamp(2000);
            </script>
            <?php
            echo "</span></b> secondes</h4><img src='./adm-auth/images/superdroid.png' /></div><div class='bas' id='bas'></div>";
        } else {

            echo "<br /><br /><h1><b style=\"color: rgb(230, 0, 17);\">Erreur de suppression de l'accompagnant</b></h1><h4>Redirection vers la page accompagnants dans <b><span id='compteur' class='compteur'>";
            ?>

            <script language='javascript'>

                function timestamp(t)
                {
                    document.getElementById("compteur").innerHTML=(t/1000);
                    t-=1000;
                    if(t==0)
                        self.location="<?php echo $page; ?>";
                                                                                                                                                                                                                                                                                                    		 
                    setTimeout("timestamp("+t+")",1000);
                }
                window.onload = timestamp(2000);
            </script>
            <?php
            echo "</span></b> secondes</h4><img src='./adm-auth/images/superdroid.png' /></div><div class='bas' id='bas'></div>";
        }



        $mysqli->close();
    } else {


        $idP = $_GET['id_participant'];
        $nomP = $_GET['nomP'];
        $prenomP = $_GET['prenomP'];
        $nb_accompagnants = $_GET['nb_accompagnants'];


        if (!isset($_GET['id'])) {
            $titre_page = "Ajouter un accompagnant";
            $bouton = "Ajouter";

            $id = -1;
            $nom_prenom = "";
            $date_naissance = "";
        }

        if (isset($_GET['nom_prenom'])) {

            $titre_page = "Modifier un accompagnant";
            $bouton = "Modifier";

            $id = $_GET['id'];
            $nom_prenom = $_GET['nom_prenom'];
            $date_naissance = $_GET['date_naissance'];
        }
        ?>
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
        <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US" xml:lang="en">
            <head>
                <!--

        Base template (without user's data) checked by http://validator.w3.org : "This page is valid XHTML 1.0 Transitional"
                -->
                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
                <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />
                <title>ISIMG Android</title>

                <link href="css/jquery-ui-1.10.0.custom.min.css" rel="stylesheet" />


                <link href="./css/screen.css" rel="stylesheet" media="screen" />


                <link rel="stylesheet" href="./css/style.css" type="text/css" media="screen" />
                <!--[if IE 6]><link rel="stylesheet" href="./css/style.ie6.css" type="text/css" media="screen" /><![endif]-->
                <!--[if IE 7]><link rel="stylesheet" href="./css/style.ie7.css" type="text/css" media="screen" /><![endif]-->

                <style>

                    .henislayout-cell, .heniscontent-layout-row
                    {
                        background-color: Transparent;
                        vertical-align: top;
                        text-align: left;
                        border: none;

                        padding:0;
                    }
                    .henisarticle td
                    {
                        padding: 5px;
                        border: solid 0px #616161;
                        vertical-align: top;
                        text-align: left;
                    }

                    .preview
                    {
                        width:200px;
                        border:solid 1px #dedede;
                        padding:10px;
                    }

                </style>




                <script type="text/javascript">
                                                                                                                                                                                                
                    function verif_formulaire(e)
                    {
                        $("#nom").html('');
                                                    
                        $("#date").html('');
                                                        
                        var returnval=emailfilter.test(e.value)
                        if(document.formulaire.nom.value == "")  {
                            $('#nom').html("Veuillez saisir le nom et le prénom!");
                            document.formulaire.nom.focus();
                            return false;
                        }
                        if(document.formulaire.date.value == "") {
                            $('#date').html("Veuillez choisir la date de naissance!");
                            document.formulaire.date.focus();
                            return false;
                        }
                                                       
                                                                
                                                                
                        return true;
                                                                                                                                                            
                                                                                                                                                                                                
                                                                                                                                                                                          
                    }

                </script>




                <script type="text/javascript" src="./js/jquery.js"></script>


                <script type="text/javascript" src="./js/script.js"></script>




                <script src="js/jquery-ui-1.10.0.custom.min.js"></script>

                <script src="js/jquery.ui.datepicker-fr.js"></script>

                <script>
                    $(function() {
                                                                    
                        $( "#datepicker" ).datepicker({
                            dateFormat : "yy-mm-dd",
                                                                
                            changeMonth: true,
                            changeYear: true,
                            yearRange : "c-60:c+60",
                            maxDate : 0
                                                                
                        });
                                            
                        $( "#datepicker" ).datepicker( $.datepicker.regional[ "fr" ] );
                                                                    
                    });
                </script>

            </head>
            <body>


                <div id="henispage-background-glare">
                    <div id="henispage-background-glare-image">
                        <div id="henismain">nomp
                            <div class="henisheader">
                                <div class="henisheader-center">
                                    <div class="henisheader-jpeg"></div>
                                </div>
                                <div class="henisheader-wrapper">
                                    <div class="henisheader-inner">
                                        <div class="henisheaderobject"></div>
                                        <div class="henislogo">
                                            <h1 id="name-text" class="henislogo-name"><a href="./index.php">Administration du site</a></h1>
                                            <h2 id="slogan-text" class="henislogo-text">ISIMG Android</h2>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div class="henisnav">
                                <div class="henisnav-l"></div>
                                <div class="henisnav-r"></div>
                                <div class="henisnav-wrapper">
                                    <div class="henisnav-inner">
                                        <ul class="henismenu">
                                            <li><a href="./index.php"><span class="l"> </span><span class="r"> </span><span class="t">Accueil</span></a></li>
                                            <li><a href="./adm_actualites.php" class="active"><span class="l"> </span><span class="r"> </span><span class="t">Actualités</span></a></li>
                                            <li><a href="./adm_sponsors.php"><span class="l"> </span><span class="r"> </span><span class="t">Sponsors</span></a></li>
                                            <li><a href="./adm_comite_pilotage.php"><span class="l"> </span><span class="r"> </span><span class="t">Comité de pilotage</span></a></li>
                                            <li><a href="./adm_comite_organisation.php"><span class="l"> </span><span class="r"> </span><span class="t">Comité d'organisation</span></a></li>
                                            <li><a href="./adm_participants.php"><span class="l"> </span><span class="r"> </span><span class="t">Participants</span></a></li>
                                            <li><a href="./adm-auth/act_authentification.php?action=logout"><span class="l"> </span><span class="r"> </span><span class="t">Déconnexion</span></a></li>
                                        </ul>
                                    </div>
                                </div>
                            </div>
                            <div class="henissheet">
                                <div class="henissheet-tl"></div>
                                <div class="henissheet-tr"></div>
                                <div class="henissheet-bl"></div>
                                <div class="henissheet-br"></div>
                                <div class="henissheet-tc"></div>
                                <div class="henissheet-bc"></div>
                                <div class="henissheet-cl"></div>
                                <div class="henissheet-cr"></div>
                                <div class="henissheet-cc"></div>
                                <div class="henissheet-body">
                                    <div class="heniscontent-layout">
                                        <div class="heniscontent-layout-row">
                                            <div class="henislayout-cell heniscontent">
                                                <div class="henispost">
                                                    <div class="henispost-tl"></div>
                                                    <div class="henispost-tr"></div>
                                                    <div class="henispost-bl"></div>
                                                    <div class="henispost-br"></div>
                                                    <div class="henispost-tc"></div>
                                                    <div class="henispost-bc"></div>
                                                    <div class="henispost-cl"></div>
                                                    <div class="henispost-cr"></div>
                                                    <div class="henispost-cc"></div>
                                                    <div class="henispost-body">
                                                        <div class="henispost-inner henisarticle">
                                                            <h2 class="henispostheader"><?php echo $titre_page; ?></h2><br /><br />
                                                            <div class="henispostcontent">
                                                                <div class="aligncenter">
                                                                    <table>
                                                                        <tbody>
                                                                            <tr>
                                                                                <td>
                                                                                    <form id="form1" name="formulaire" action="act_accompagnants.php" method="post">	

                                                                                        <fieldset>
                                                                                            <p class="first">
                                                                                                <label for="name">Nom et Prénom</label>
                                                                                                <div id="nom"></div>
                                                                                                <input type="text" name="nom_prenom" size="100" value="<?php echo $nom_prenom; ?>" />

                                                                                            </p>

                                                                                            <p class="first">



                                                                                                <label for="date">Date de naissance</label>
                                                                                                <div id="date"></div>
                                                                                                <input type="text" name="date_naissance" id="datepicker" size="100" value="<?php echo $date_naissance; ?>" />

                                                                                            </p>


                                                                                        </fieldset>
                                                                                        <fieldset>

                                                                                            <p class="first">														
                                                                                                <label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
                                                                                            </p>

                                                                                        </fieldset>
                                                                                        <div id="preview"><img src="adm-auth/images/superdroid.png"</div>

                                                                                        <input type="hidden" name="id" value="<?php echo $id; ?>" />
                                                                                        <input type="hidden" name="id_participant" value="<?php echo $idP; ?>" />
                                                                                        <input type="hidden" name="nomP" value="<?php echo $nomP; ?>" />
                                                                                        <input type="hidden" name="prenomP" value="<?php echo $prenomP; ?>" />
                                                                                        <input type="hidden" name="nb_accompagnants" value="<?php echo $nb_accompagnants; ?>" />

                                                                                        <p class="submit">
                                                                                            <button type="submit" onClick="return verif_formulaire()"><?php echo $bouton; ?></button>
                                                                                        </p>



                                                                                    </form>
                                                                                </td>
                                                                            </tr>
                                                                        </tbody>
                                                                    </table>


                                                                </div>
                                                            </div>
                                                            <div class="cleared"></div>
                                                        </div>
                                                        <div class="cleared"></div>
                                                    </div>
                                                </div>
                                                <div class="cleared"></div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="cleared"></div>
                                    <div class="cleared"></div>
                                </div>
                            </div>
                            <div class="henisfooter">
                                <div class="henisfooter-t"></div>
                                <div class="henisfooter-b"></div>
                                <div class="henisfooter-body">
                                    <div class="henisfooter-text">
                                        <div class="cleared"></div>
                                        <p class="henispage-footer">Auteur : <a href="http://www.isimg.rnu.tn/fra/profil/heni_saber">henis</a>.</p>
                                    </div>
                                    <div class="cleared"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>


            </body>
        </html>
        <?php
    }
}
?>